﻿namespace td.Core.Xml
{
    using System;
    using System.Collections.Generic;
    using System.Xml;
    using System.Windows.Forms;
    using System.IO;

    public class AddIn : IXmlStorage
    {
        private string addInFileName = null;
        private List<AutoStartXml> autostarts = new List<AutoStartXml>();
        private string copyright = "";
        private string date = "";
        public static readonly string DefaultConfigXml = (Application.StartupPath + Path.DirectorySeparatorChar + "addins.xml");
        private bool enabled;
        private static AddIn instance = null;
        private string name = "";
        private List<PadXml> pads = new List<PadXml>();
        private List<RuntimeXml> runtimes = new List<RuntimeXml>();
        private List<string> stringResources = new List<string>();
        private string url = "";
        private string version = "";
        private XmlDocument xmldoc = new XmlDocument();

        private AddIn()
        {
            this.ToolButtons = new List<ToolButtonXml>();
            
        }

        public void Clear()
        {
            this.runtimes.Clear();
            this.autostarts.Clear();
            this.pads.Clear();
            this.ToolButtons.Clear();
        }

        public object CreateObject(string className)
        {
            foreach (RuntimeXml xml in this.runtimes)
            {
                object obj2 = xml.CreateInstance(className);
                if (obj2 != null)
                {
                    return obj2;
                }
            }
          //  LoggingService.Error("无法创建对象: " + className);
            return null;
        }

        //public ContextMenuXml GetContextMenuXml(string contextID)
        //{
        //    if ((contextID != null) && !contextID.Equals(""))
        //    {
        //        foreach (ContextMenuXml xml in this.contexts)
        //        {
        //            if (string.Compare(xml.ID, contextID, true) == 0)
        //            {
        //                return xml;
        //            }
        //        }
        //    }
        //    return null;
        //}

        public static AddIn GetInstance()
        {
            if (instance == null)
            {
                instance = new AddIn();
            }
            return instance;
        }

        public void GetValueFromXml(XmlNode objectNode)
        {
            if ((objectNode != null) && objectNode.Name.ToLower().Equals("addin"))
            {
                XmlAttributeCollection attributes = objectNode.Attributes;
                for (int i = 0; i < attributes.Count; i++)
                {
                    XmlAttribute attribute = attributes[i];
                    if (attribute.Name.ToLower().Equals("name"))
                    {
                        this.name = attribute.Value;
                    }
                    if (attribute.Name.ToLower().Equals("version"))
                    {
                        this.version = attribute.Value;
                    }
                    if (attribute.Name.ToLower().Equals("copyright"))
                    {
                        this.copyright = attribute.Value;
                    }
                    if (attribute.Name.ToLower().Equals("url"))
                    {
                        this.url = attribute.Value;
                    }
                    if (attribute.Name.ToLower().Equals("date"))
                    {
                        this.date = attribute.Value;
                    }
                }
                this.Clear();
                XmlNodeList childNodes = objectNode.ChildNodes;
                if ((childNodes != null) && (childNodes.Count > 0))
                {
                    for (int j = 0; j < childNodes.Count; j++)
                    {
                        XmlNode node = childNodes[j];
                        if (node.Name.ToLower().Equals("runtime"))
                        {
                            RuntimeXml item = new RuntimeXml();
                            item.GetValueFromXml(node);
                            this.runtimes.Add(item);
                        }
                        if (node.Name.ToLower().Equals("autostart"))
                        {
                            AutoStartXml xml2 = new AutoStartXml();
                            xml2.GetValueFromXml(node);
                            this.autostarts.Add(xml2);
                        }
                        if (node.Name.ToLower().Equals("pad"))
                        {
                            PadXml xml3 = new PadXml();
                            xml3.GetValueFromXml(node);
                            this.pads.Add(xml3);
                        }
               
                        if (node.Name.ToLower().Equals("toolbutton"))
                        {
                            ToolButtonXml xml5 = new ToolButtonXml();
                            xml5.GetValueFromXml(node);
                            this.ToolButtons.Add(xml5);
                        }
            
                    }
                }
            }
        }

        public bool LoadFromFile(string xmlPath)
        {
            try
            {
                this.addInFileName = xmlPath;
                this.xmldoc.Load(xmlPath);
                XmlNode objectNode = this.xmldoc.SelectSingleNode("//AddIn");
                if (objectNode != null)
                {
                    this.GetValueFromXml(objectNode);
                }
            }
            catch (Exception exception)
            {
                string message = "加载配置文件失败，发生异常：" + exception.Message;
               // LoggingService.Info(message);
                throw new Exception(message);
            }
            return true;
        }

        public bool SaveToFile(string xmlPath)
        {
            try
            {
                this.xmldoc = new XmlDocument();
                XmlDeclaration newChild = this.xmldoc.CreateXmlDeclaration("1.0", "GB2312", null);
                this.xmldoc.AppendChild(newChild);
                this.SetValueToXml(this.xmldoc, null);
                this.xmldoc.Save(xmlPath);
                return true;
            }
            catch (XmlException exception)
            {
              //  LoggingService.Info("保存配置文件失败，发生异常：" + exception.Message);
                return false;
            }
        }

        public void SetValueToXml(XmlDocument xmldoc, XmlElement parentEle)
        {
            XmlElement newChild = xmldoc.CreateElement("AddIn");
            newChild.SetAttribute("name", this.name);
            newChild.SetAttribute("version", this.version);
            newChild.SetAttribute("copyright", this.copyright);
            newChild.SetAttribute("url", this.url);
            newChild.SetAttribute("date", this.date);
            xmldoc.AppendChild(newChild);
            for (int i = 0; i < this.runtimes.Count; i++)
            {
                this.runtimes[i].SetValueToXml(xmldoc, newChild);
            }
            for (int j = 0; j < this.autostarts.Count; j++)
            {
                this.autostarts[j].SetValueToXml(xmldoc, newChild);
            }
            for (int k = 0; k < this.pads.Count; k++)
            {
                this.pads[k].SetValueToXml(xmldoc, newChild);
            }
            for (int n = 0; n < this.ToolButtons.Count; n++)
            {
                this.ToolButtons[n].SetValueToXml(xmldoc, newChild);
            }
        }

        public override string ToString()
        {
            return ("[AddIn: " + this.Name + "]");
        }

        public List<AutoStartXml> AutoRun
        {
            get
            {
                return this.autostarts;
            }
            set
            {
                this.autostarts = value;
            }
        }

 
        //public List<ContextMenuXml> ContextMenus
        //{
        //    get
        //    {
        //        return this.contexts;
        //    }
        //    set
        //    {
        //        this.contexts = value;
        //    }
        //}

        public string CopyRight
        {
            get
            {
                return this.copyright;
            }
            set
            {
                this.copyright = value;
            }
        }

        public bool Enabled
        {
            get
            {
                return this.enabled;
            }
            set
            {
                this.enabled = value;
            }
        }

        public string FileName
        {
            get
            {
                return this.addInFileName;
            }
            set
            {
                this.addInFileName = value;
            }
        }



        public string ModifyDate
        {
            get
            {
                return this.date;
            }
            set
            {
                this.date = value;
            }
        }

        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
            }
        }

        public List<PadXml> Pads
        {
            get
            {
                return this.pads;
            }
            set
            {
                this.pads = value;
            }
        }

        public List<RuntimeXml> Runtimes
        {
            get
            {
                return this.runtimes;
            }
            set
            {
                this.runtimes = value;
            }
        }

        public List<string> StringResources
        {
            get
            {
                return this.stringResources;
            }
            set
            {
                this.stringResources = value;
            }
        }

        public List<ToolButtonXml> ToolButtons
        {
            get;
            set;
        }

        public string URL
        {
            get
            {
                return this.url;
            }
            set
            {
                this.url = value;
            }
        }

        public string Version
        {
            get
            {
                return this.version;
            }
            set
            {
                this.version = value;
            }
        }

    }
}

